//
//  KindsView.m
//  cardmenNew
//
//  Created by Dengzeyu on 15/12/30.
//  Copyright © 2015年 Dengzeyu. All rights reserved.
//

#import "KindsView.h"
#import "RNGridMenu.h"
#import "MyView.h"
#import "GetURLContent.h"
#import "jingtai.h"
#import "/Users/dengzeyu/Documents/projectiOS /cardmenNew/cardmenNew/Helper/UIImage_imageScale.h"
#import "ContentViewController.h"
#import "UIDeviceMeasure1.h"

@implementation KindsView
-(void)viewDidLoad
{
         self.automaticallyAdjustsScrollViewInsets =NO;
    _appList = [[NSMutableArray alloc] init];
    
    [jingtai sharedSingleton].kindview = self;
    [super viewDidLoad];

    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    self.myTopic = [[DZYTopic alloc] init];
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect topicrect =CGRectMake(0,rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.width*0.4375);
    self.myTopic.frame =topicrect;
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect pagerect =CGRectMake(0, self.myTopic.frame.size.height+rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.myTopic.frame.size.height*0.1);
    self.page = [[UIPageControl alloc] init];
    [self.page setFrame:pagerect];
    
    
    self.topicArray = [[NSMutableArray alloc] init];
    [self.topicArray addObject:[NSDictionary dictionaryWithObjects:@[@"http://163.54114.com/upimg/allimg/120619/5-120619112512.jpg" ,@"PIC3",@NO,@"http://www.yunyankeji.com"] forKeys:@[@"pic",@"title",@"isLoc",@"URL"]]];
    //网络图片加载失败
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    
    NSLog(@"%@",self.topicArray);
    //加入数据
    [self.myTopic setSelf];
    self.myTopic.pics = self.topicArray;
    [self.myTopic upDate];
    self.myTopic.DZYdelegate = self;
    self.myTopic.clipsToBounds = YES;
    self.myMainscroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myTopic.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.navigationController.navigationBar.clipsToBounds =YES;
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.Settingitem.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.downloaditem.image =[[UIImage imageNamed:@"history"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    CGRect myrect = CGRectMake(0, self.myTopic.frame.size.height+self.page
                               .frame.size.height+self.navigationController.navigationBar.frame.size.height+rectStatus.size.height, self.view.frame.size.width, self.view.frame.size.height-self.myTopic.frame.size.height-self.page.frame.size.height-self.navigationController.navigationBar.frame.size.height-rectStatus.size.height-self.tabBarController.tabBar.frame.size
                               .height-self.myTopic.frame.size.height/3.0);
    self.myMainscroll = [[UIScrollView alloc]initWithFrame:myrect];
    [self.page setNumberOfPages:self.topicArray.count];
    self.page.currentPage = 0;
    self.page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:self.myMainscroll];
    self.serchButton =[[UIButton alloc] init];
    CGRect searchButtonRect = CGRectMake(0, self.myMainscroll.frame.origin.y+self.myMainscroll.frame.size.height, self.view.frame.size.width, self.myTopic.frame.size.height/3.0);
    self.serchButton.frame =searchButtonRect;
    UIImage * searchimage = [UIImage imageNamed:@"search"];
    searchimage = [searchimage TransformtoSize:CGSizeMake(self.myMainscroll.frame.size.width/2, self.serchButton.frame.size.height)];
    [self.serchButton setImage:searchimage forState:UIControlStateNormal];
    [self.view addSubview:self.serchButton];
    GetURLContent * url =[[GetURLContent alloc] init];
    [url Load:[[jingtai sharedSingleton].mainIP stringByAppendingString:@"/cardmennew/getkinds.aspx"]andkind:@"kind"];
  
    
}
- (IBAction)hasbeendownload:(id)sender {
    
    ContentViewController * ck ;
    UIDeviceMeasure1 *device = [[UIDeviceMeasure1 alloc]init];
    switch ([device identifyResolution ]) {
        case UIDevice_iPad:
        {
            NSLog(@"ipad");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"ipad"];
            
            break;
            
        }
            
        case UIDevice_iPhone4_4s:
        {
            NSLog(@"iphone4");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone4"];
            
            break;
            
        }
        case UIDevice_iPhone5_5s:
        {
            NSLog(@"ip5");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone5"];
            
            break;
        }
        case UIDevice_iPhone6:
        {
            NSLog(@"ip6");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone6"];
            
            break;
        }
        case UIDevice_iPhone6plus:
        {
            NSLog(@"ip6p");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"ChoseViewcontroller"];
            
            break;
        }
            
    }
    ck.kindname= [jingtai sharedSingleton].shopview.kind;
    ck.leixing =1;
        [self presentViewController:ck animated:YES completion:nil];
}
- (IBAction)gotosetting:(id)sender {
}
-(void)drawLayer
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);//线条颜色
    CGContextMoveToPoint(context, self.view.frame.size.width/2.0, self.myMainscroll.frame.origin.y+self.myTopic.frame.size.height/10);
    CGContextAddLineToPoint(context, self.view.frame.size.width/2.0,self.serchButton.frame.origin.y-self.myTopic.frame.size.height/10);
    CGContextStrokePath(context);
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.myTopic removeFromSuperview];
    [self.page removeFromSuperview];
   [self.navigationItem setHidesBackButton:YES];
    self.title =@"主页";
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    self.myTopic = [[DZYTopic alloc] init];
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect topicrect =CGRectMake(0,rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.width*0.4375);
    self.myTopic.frame =topicrect;
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect pagerect =CGRectMake(0, self.myTopic.frame.size.height+rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.myTopic.frame.size.height*0.1);
    self.page = [[UIPageControl alloc] init];
    [self.page setFrame:pagerect];
    
    
    self.topicArray = [[NSMutableArray alloc] init];
    [self.topicArray addObject:[NSDictionary dictionaryWithObjects:@[@"http://163.54114.com/upimg/allimg/120619/5-120619112512.jpg" ,@"PIC3",@NO,@"http://www.yunyankeji.com"] forKeys:@[@"pic",@"title",@"isLoc",@"URL"]]];
    //网络图片加载失败
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    
    NSLog(@"%@",self.topicArray);
    //加入数据
    [self.myTopic setSelf];
    self.myTopic.pics = self.topicArray;
    [self.myTopic upDate];
    self.myTopic.DZYdelegate = self;
    self.myTopic.clipsToBounds = YES;
     self.myTopic.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.page setNumberOfPages:self.topicArray.count];
    self.page.currentPage = 0;
    self.page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:self.myTopic];
    [self.view addSubview: self.page];
      [self drawLayer];
}
-(void) loadscroll
{
    int totalColumns = 2;
    
    NSLog(@"%f   scroll kuandu",self.myTopic.frame.size.width);
    CGFloat Y =0;
    CGFloat W = self.myMainscroll.frame.size.width/2.0;
    CGFloat H = self.myMainscroll.frame.size.height/2.0;
    CGFloat X = (self.view.frame.size.width - totalColumns * W) / (totalColumns + 1);
    for ( int index = 0; index <_appList.count ; index++) {
        
        Model *model = _appList[index];
        int row = index / totalColumns;
        int col = index % totalColumns;
        CGFloat viewX = X + col * (W + X);
        
        CGFloat viewY = 0 + row * (H + Y);
        MyView *view = [[MyView alloc] initWithFrame:CGRectMake(viewX, viewY, W, H) Model:model MyButtonBlock:^{
            
//            _myLabel = [[MyLabel alloc] init];
//            
//            [self.view addSubview:_myLabel];
            
            NSLog(@"block");
        }];
        view.model = model;
        
            [self.myMainscroll addSubview:view];
        //_scroview.contentSize =  CGSizeMake(0, 266);
        CGFloat maxY = CGRectGetMaxY(view.frame);
        self.myMainscroll.contentSize = CGSizeMake(0, maxY);
    }
    [jingtai setlocalkindVersion:[jingtai sharedSingleton].kindversion];
}
-(void)viewDidAppear:(BOOL)animated
{
    
}

-(void)didClick:(int)data
{
    NSString*url=[[self.topicArray objectAtIndex:(data)] objectForKey:@"URL"];
    NSLog(@"%@",url);
    NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
    if ([[UIApplication sharedApplication] canOpenURL:cleanURL]) {
        // 如果已经安装淘宝客户端，就使用客户端打开链接
        [[UIApplication sharedApplication] openURL:cleanURL];
    } else {
        // 否则使用 Mobile Safari 或者内嵌 WebView 来显示
        NSMutableString *mstr=[NSMutableString stringWithString:url];
        NSArray*temp=[url componentsSeparatedByString:@":"];
        NSRange substr = [url rangeOfString:[temp objectAtIndex:0]];
        if (substr.location != NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:@"http"];
            NSLog(@"%@",mstr);
        }
        cleanURL=[NSURL URLWithString:mstr];
        [[UIApplication sharedApplication] openURL:cleanURL];
    }
    
}

-(void)currentPage:(int)page total:(NSUInteger)total
{
   self.page.numberOfPages = total;
self.page.currentPage = page;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
@end
